<?php
namespace app\admin2\controller;

use think\Db;
use app\common\controller\AdminBaseo;
use app\common\model\Users as UsersModel;
use app\common\model\Csuser as CsuserModel;

class ChatMonitor extends AdminBaseo
{
    protected $users_model;
    protected $csuser_model;

    protected function _initialize(){
        parent::_initialize();
        $this->csuser_model = new CsuserModel;
        $this->users_model = new UsersModel;
    }

    public function index($butt_id){
        return $this->fetch();
    }

    /**
     * 获取对应客服的客户列表
     */
    public function getAppointUsersList(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $butt_id = $data['butt_id'];
            $result = $this->users_model->field('butt_id,openid,nickname,subscribe_time,subscribe,create_butt_time,headimgurl')->where('butt_id',$butt_id)->order('subscribe_time desc')->select();
            return json($result);
        }
    }

    /**
     * 获取聊天记录
     */
    public function getAppointUserChat(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $butt_id = $data['butt_id'];
            $openid = $data['openid'];
            $sqlname = 'chatlog'.$butt_id % 10;
            $result = Db::connect('db_config_chatlog')->name($sqlname)->where('cs_id',$butt_id)->where('openid',$openid)->order('send_time asc')->select();
            $customerService = $this->csuser_model->where('id',$butt_id)->find();
            return json(['chatlog'=>$result,'customerServiceInfo'=>$customerService]);
        }

    }
}